﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Smart_Archives.Entity;
using Smart_Archives.Models;
using Smart_Archives.Models.Enum;
using Smart_Archives.Utils;

namespace Smart_Archives.Controllers
{
    /// <summary>
    /// 后台管理首页控制器
    /// </summary>
    public class IndexController : Controller
    {
        private readonly SaContext _saContext;

        public IndexController(SaContext saContext)
        {
            _saContext = saContext;
        }

        public ActionResult Index()
        {
            var categories = GetCategoryAndDevice();
            return View(categories);
        }

        public ViewResult Welcome()
        {
            return View();
        }


        public List<Category> GetCategoryAndDevice()
        {
            var rootList = new List<Category>();
            //获取header模块中列表
            //获取四大基本类型的数据
            var rootCategories = _saContext.SecurityDeviceCategory.Where(a => a.ParentId == 0).ToList();
            foreach (var rootItem in rootCategories)
            {
                var parentCategoriesList = new List<Category>();
                var model = new Category { Id = rootItem.Id, Name = rootItem.Name, ChildItems = parentCategoriesList };
                //获取基类父级分组
                var parentCategories = _saContext.SecurityDeviceCategory.Where(a => a.ParentId == rootItem.Id&&a.Status==(int)StatusEnum.Use).ToList();
                foreach (var parentItem in parentCategories)
                {
                    var childDevicesList = new List<Category>();
                    var parentModel = new Category { Id = parentItem.Id, Name = parentItem.Name, ChildItems = childDevicesList,Type = parentItem.Type};
                   //获取分组下设备
                    var devices = _saContext.SecurityDevice
                        .Where(a => a.SecurityCategoryId == parentItem.Id && a.Status == 1).ToList();
                    foreach (var childItem in devices)
                    {
                        var childModel = new Category { Id = childItem.Id, Name = childItem.Name };
                        childDevicesList.Add(childModel);
                    }
                    parentCategoriesList.Add(parentModel);
                }
                rootList.Add(model);
            }

            
            return rootList;
        }


    }
}